package com.gtq.uhome.service.serviceImpl;

import com.gtq.uhome.mapper.OrderMapper;
import com.gtq.uhome.service.OrderService;
import com.gtq.uhome.util.UtilsForString;
import com.gtq.uhome.vo.AllOrderInfor;
import com.gtq.uhome.vo.OrderInfor;
import com.gtq.uhome.vo.OrderOneToThree;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author ylhy
 * @Describe
 * @create 2020-08-06 9:30
 */
@Service("OrderService")
public class OrderServiceImpl implements OrderService {

    @Resource
    OrderMapper orderMapper;

    @Override
    public List<AllOrderInfor> findAllOrderListByOrderLg() {

        List<AllOrderInfor> allOrderInforList = orderMapper.findAllOrderListByOrderLg();
        System.out.println("所有的订单 : "+allOrderInforList);
        return allOrderInforList;
    }

    @Override
    public List<AllOrderInfor> findAllOrderListByOrderLgFromSite(Integer siteId) {
        List<AllOrderInfor> allOrderInforListFromSite = orderMapper.findAllOrderListByOrderLg(siteId);
        System.out.println("所有的订单 : "+allOrderInforListFromSite);
        return allOrderInforListFromSite;
    }


    @Override
    public List<AllOrderInfor> findSomeOrderList(String keyWords, String deliveryTimestart, String deliveryTimeend,
                                                 String checkStatus, String siteName,String singCode,String agingCode) {

        List<AllOrderInfor> SomeOrderInforList = orderMapper.findSomeOrderList(keyWords,deliveryTimestart,
                deliveryTimeend,checkStatus,siteName,singCode,agingCode);
        return SomeOrderInforList;
    }

    @Override
    public OrderInfor findOneOrderListByOrderLg(String logisticsId) {
        System.out.println("----------"+logisticsId+"-----------");
        OrderInfor orderInfor = orderMapper.findOneOrderListByOrderLg(logisticsId);

        List<OrderOneToThree> orderOneToThree = orderMapper.OneToThree(logisticsId);

        orderInfor.setOrderOneToThreeList(orderOneToThree);

        return orderInfor;
    }



    @Override
    @Transactional
    public Integer delteOrderbyLg(String id, String lgId1) {
        //先按照物流单进行假删除

        Integer lines = 0;

        lines = orderMapper.delteOneOrderbyLg(lgId1);
        System.out.println("删除了 : "+lgId1+" 的运单下的 "+lines +" 笔商品");

        // 判断该订单还有没有没有删除的运单
        if (lines == 0){
            Integer lgs = orderMapper.findlg(id);
            System.out.println("订单编号为 : "+id+" 的订单下还有 "+lgs+" 笔商品");
            // 判断是否还有其他物流单号
            if(lgs != 0) {
                System.out.println("订单编号为 : "+id+" 的订单下还有 "+lgs+" 笔商品  被删除了之后");
                Integer lg2 = orderMapper.findlg(id);
                System.out.println("还有 : "+lg2+" 笔商品");
                // 如果还有就返回
                if (lg2 == 0){
                    System.out.println("订单编号为  "+id+" 的订单下没有商品 : 该删除订单了");
                    Integer delOrder = orderMapper.delOrder(id);
                    System.out.println("删除了 : "+id+" 订单");
                    return lines;
                }else {// 没有就就删除订单
                    System.out.println("查出订单编号 : "+id+" 下至少还有 : 1 笔商品 后返回con删除的云答案影响的行数为 :"+ lines);
                    return lines;
                }
            }
        }

        return lines;
    }



}
